package jh.com.jhappbf.utils;

import android.util.Log;

import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import jh.com.jhappbf.model.DanhaoModel;
import jh.com.jhappbf.model.PihaoModel;

public class JsonHelper {
    public static Class<?> kindClass;

    /**
     * @param JsonString 一个json格式的字符串
     * @Summary 获取一个json对象
     * @return 正常返回一个json对象 异常返回 null
     */
    public static synchronized JSONObject getJsonObject(String JsonString) {
        JSONObject jsonObject = null;
        try {
            JsonString = getJsonStrFromNetData(JsonString);
            JSONArray entries = new JSONArray(JsonString);
            if (entries.length() > 0) {
                jsonObject = entries.getJSONObject(0);
            }
            return jsonObject;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * @param jsonString json格式的字符串
     * @return 返回Json 对象的数组
     * @Summary 获取json 对象的数组
     */
    public static synchronized List<JSONObject> getJsonObjects(String jsonString) {
        jsonString = getJsonStrFromNetData(jsonString);
        ArrayList<JSONObject> array = new ArrayList<>();
        try {
            JSONArray entries = new JSONArray(jsonString);
            for (int i = 0; i < entries.length(); i++) {
                JSONObject jsObject = entries.getJSONObject(i);
                if (jsObject != null) {
                    array.add(jsObject);
                }
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
        return array;
    }

    /**
     * @param jsonString 包含Json字符串的数据
     * @return json字符串
     * @summary 去除非Json的字符串部分
     */
    public static synchronized String getJsonStrFromNetData(String jsonString) {
        int first = jsonString.indexOf("[");
        int last = jsonString.lastIndexOf("]");
        String result = "";
        if (last > first) {
            result = jsonString.substring(first, last + 1);
        }
        return result;
    }


    /***
     * @summary 通过json字符串获取 实体对象
     * @param jsonStr Json 字符串
     * @return 实体数组
     */
    public static synchronized <T> ArrayList<T> getEntityFromJson(String jsonStr, Class<T> classOfT) {
        try {
            jsonStr = getJsonStrFromNetData(jsonStr);
            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<PihaoModel>>() {}.getType();
            ArrayList<PihaoModel> jsObjs = gson.fromJson(jsonStr, type);
//            ArrayList<T> listOfT = new  ArrayList<T>();
//            for (JsonObject obj : jsObjs) {
//                listOfT.add(new Gson().fromJson(obj,classOfT));
//            }
//            return  listOfT;
            return (ArrayList<T>) jsObjs;
        } catch (Exception e) {
            return null;
        }
    }
    public static synchronized <T> ArrayList<T> getEntityFromJsonDanhaoModel(String jsonStr, Class<T> classOfT) {
        try {
            jsonStr = getJsonStrFromNetData(jsonStr);
            Gson gson = new Gson();
            Type type = new TypeToken<ArrayList<DanhaoModel>>() {}.getType();
            ArrayList<DanhaoModel> jsObjs = gson.fromJson(jsonStr, type);
//            ArrayList<T> listOfT = new  ArrayList<T>();
//            for (JsonObject obj : jsObjs) {
//                listOfT.add(new Gson().fromJson(obj,classOfT));
//            }
//            return  listOfT;
            return (ArrayList<T>) jsObjs;
        } catch (Exception e) {
            Log.d("151225", "getEntityFromJsonDanhaoModel: "+e.toString());
            return null;
        }
    }
    public static String toJson(Object obj,int method) {
        // TODO Auto-generated method stub
        if (method==1) {//字段是首字母小写，其余单词首字母大写
            Gson gson = new Gson();
            String obj2 = gson.toJson(obj);
            return obj2;
        }else if(method==2){
//　　　　　　 FieldNamingPolicy.LOWER_CASE_WITH_DASHES    全部转换为小写，并用空格或者下划线分隔
//   　　　　 FieldNamingPolicy.UPPER_CAMEL_CASE    所以单词首字母大写
            Gson gson2=new GsonBuilder().setFieldNamingPolicy(FieldNamingPolicy.UPPER_CAMEL_CASE).create();
            String obj2=gson2.toJson(obj);
            return obj2;
        }
        return "";
    }
}
